package com.jr.javaproject.request;
/*
 *  @author:Fangrongxin
 *  @email:204271145@qq.com
 *  @desc:该类用于封装提供网络请求的功能代码，用于发起网络请求，并将网络的数据进行返回
 *  @datetime:2021/12/23 9:50
 */

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

public class HttpUtils {


    public static String get(String path) {
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;

        String line = "";//最开始空行
        String result = "";//每次读取到以后都要累计起来的读取结果
        try {
            //Union Resource Location 统一资源定位
            URL url = new URL(path);
            URLConnection conn = url.openConnection();
            //进行连接
            conn.connect();

            inputStream = conn.getInputStream();

            //字符流：按行读

            reader = new BufferedReader(new InputStreamReader(inputStream));
//            String line = reader.readLine();
//            while (line !=null){
//                reader.readLine();
//            }
            while ((line = reader.readLine()) != null) {
                result = result + line;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭读取流
            try {
                if (reader != null) {
                    reader.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static String post(String path, Map<String, String> header, byte[] data) {
        OutputStream outputStream = null;
        BufferedReader reader = null;
        //result表示最后要返回的数据
        String result = "";

        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();//自动装箱
            conn.setRequestMethod("POST");
            //设置可以发送数据和接收数据
            conn.setDoInput(true);
            conn.setDoOutput(true);

            //设置请求头的属性
            //ContentType:application/json
            //Authorization:"Basic "+ base64(用户信息)
            if (!header.isEmpty()) {//isEmpty():空
                //将header中的数据遍历设置到请求头当中
                //如何遍历map
//                String value = header.get("key");
                //如何拿到map中所有的key
                Set<String> keys = header.keySet();//keyset集合
                //如何遍历set?foreach;迭代器
                for (String key : keys) {
                    String value = header.get(key);
                    conn.setRequestProperty(key, value);
                }
            }

            conn.connect();

            //把请求数据发送到请求的服务端上
            outputStream = conn.getOutputStream();
            outputStream.write(data);
            outputStream.flush();

            //等待接受返回的数据
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String Line = null;
            while ((Line = reader.readLine()) != null) {
                result += Line;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

}
